package com.leetcode.sorts.back_track;

import sun.jvm.hotspot.utilities.soql.JSJavaVM;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: WuPeng
 * @Date: 2021/6/15 7:29 下午
 *
 * 17.电话号码的字母组合
 *
 */
public class LetterCombinations_17 {

    List<String> res = new ArrayList<>();

    String[] letters = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

    public List<String> letterCombinations(String digits) {
        StringBuffer sb = new StringBuffer();
        backTracking(digits, sb, 0);
        return res;
    }

    private void backTracking(String digits, StringBuffer sb, int index) {
        if (index == digits.length()){
            if (!sb.toString().equals("")){
                res.add(sb.toString());
            }
            return;
        }
        String letter = letters[digits.charAt(index) - '0'];
        for (int i = 0; i < letter.length(); i++) {
            sb.append(letter.charAt(i));
            backTracking(digits, sb, index+1);
            sb.deleteCharAt(sb.length()-1);
        }
    }

    public static void main(String[] args) {
        LetterCombinations_17 letter = new LetterCombinations_17();
        List<String> list = letter.letterCombinations("23");
        System.out.println(Arrays.toString(list.toArray()));
    }

}
